package com.mali.malibackendproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mali.backendcommon.common.Result;
import com.mali.backendcommon.constant.AuthenticationConstant;
import com.mali.backendcommon.constant.MessageConstant;
import com.mali.backendcommon.exception.ShuMaException;
import com.mali.backendcommon.rpc.ProductService;
import com.mali.backendcommon.rpc.UserService;
import com.mali.backendcommon.util.UserContext;
import com.mali.malibackendmodel.pojo.Product;
import com.mali.malibackendmodel.pojo.ProxyProduct;
import com.mali.malibackendmodel.pojo.Scratchpad;
import com.mali.malibackendmodel.pojo.User;
import com.mali.malibackendmodel.vo.PageVO;
import com.mali.malibackendmodel.vo.ProxyByProductIdVO;
import com.mali.malibackendmodel.vo.ProxyUserVO;
import com.mali.malibackendproduct.service.ProxyProductService;
import com.mali.malibackendproduct.mapper.ProxyProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 132993
* @description 针对表【proxy(代理)】的数据库操作Service实现
* @createDate 2024-06-16 10:20:11
*/
@Service
public class ProxyProductServiceImpl extends ServiceImpl<ProxyProductMapper, ProxyProduct>
    implements ProxyProductService {

    @Autowired
    private ProxyProductMapper proxyProductMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    @Override
    public Result<List<ProxyByProductIdVO>> getProxiesByProductId(Long productId) {
        User user = UserContext.getUser();
        Long userId=user.getId();
        Integer role=user.getRole();
        if (role== AuthenticationConstant.ADMIN){    //管理员有权查看
            // 验证权限
            boolean authority = productService.haveAuthority(productId, userId);   //创建者和代理人有权查看
            if (!authority){
                throw new ShuMaException(MessageConstant.LOW_PERMISSION);
            }
        }
        QueryWrapper<ProxyProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productId", productId);
        List<ProxyByProductIdVO> proxyUserVOList=new ArrayList<>();
        List<ProxyProduct> proxyProductList=proxyProductMapper.selectList(queryWrapper);
        for(ProxyProduct proxyProduct:proxyProductList){
            ProxyByProductIdVO proxyByProductIdVO=new ProxyByProductIdVO();
            proxyByProductIdVO.setUserId(proxyProduct.getUserid());
            proxyByProductIdVO.setUserName(userService.getByUserId(proxyProduct.getUserid()).getUserName());
            proxyUserVOList.add(proxyByProductIdVO);
        }
        return Result.success(proxyUserVOList);
    }

    @Override
    public Result deleteProxy(Long productId,Long userId) {
        //查看当前用户id是否和商品表中创建者id相同
        if(!UserContext.getUser().getId().equals(productService.getAdminUserIdByProductId(productId))){
            throw new ShuMaException(MessageConstant.PERMISSION_MISMATCH);
        }
        QueryWrapper<ProxyProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productId", productId).eq("userId",userId);
        proxyProductMapper.delete(queryWrapper);
        return Result.success();
    }

    @Override
    public Result addProxy(Long productId,Long userId) {
        //查看当前用户id是否和商品表中创建者id相同
        if(!UserContext.getUser().getId().equals(productService.getAdminUserIdByProductId(productId))){
            throw new ShuMaException(MessageConstant.PERMISSION_MISMATCH);
        }
        //判断添加的代理人的用户是否存在
        if(userService.getByUserId(userId)==null){
            throw new ShuMaException(MessageConstant.USER_NOT_EXIST);
        }
        //添加代理人是要判断其是否已是代理人
        if(productService.haveAuthority(productId, userId)){
            throw new ShuMaException(MessageConstant.ADD_PROXY_AGAIN);
        }
        ProxyProduct proxyProduct=new ProxyProduct();
        proxyProduct.setUserid(userId);
        proxyProduct.setProductId(productId);
        proxyProduct.setCreateTime(LocalDateTime.now());
        proxyProductMapper.insert(proxyProduct);
        return Result.success();
    }
}




